﻿'use strict';

define([
    'backbone',
], function (Backbone) {
    return Backbone.Router.extend({
        currentView: null,
        mainViewContainer: null,
        views: {},
        mainTitleContainer: null,

        initialize: function (options) {
            options || (options = {});

            this.mainViewContainer = $(options.container);

            this.mainTitleContainer = $(options.titleContainer);


            //Get routes passed and build the correponding routes object.
            if (options.routesConfig)
            {
                this.routesConfig = options.routesConfig;

                for (var routeName in this.routesConfig)
                    this.route(this.routesConfig[routeName], routeName, this._handleRoute);
            }


            // Prevent undesired navigation with anchors.
            $(document).on('click', 'a[data-bypass="true"]', function (evt) {
                evt.preventDefault();
            });

            return this;
        },

        execute: function (callback, args, name) {
            callback.apply(this, [this.routesConfig[name], name].concat(args));
        },
        _handleRoute: function () {
            this.switchView.apply(this, arguments);
        },
        //route: function (route, name, callback) {
        //    this.switchView(route, name, callback);

        //    this.navigate(route);

        //    if (callback)
        //        callback();

        //    return this;
        //},
        switchView: function (route, name) {
            var that = this,
                viewModuleName = 'screens/' + name + '/' + name + '.view',
                modelModuleName = 'screens/' + name + '/' + name + '.model',
                routeArgs = (arguments.length > 2) ? $(arguments).slice(2) : null;

            require(
                [viewModuleName, modelModuleName], 
                function (viewCtr, modelCtr) {
                    var view;

                    if (that.views[route] == null) {
                        view = new viewCtr({
                            id: route,
                            model: new modelCtr(),
                        });

                        that.views[route] = view;

                        view.$el.hide();

                        that.mainViewContainer.append(view.el);

                        view.render();
                    }
                    else {
                        view = that.views[route];
                    }

                    if (that.currentView != null)
                        that.currentView.hide();

                    that.currentView = view;



                    //Set website title with view title.
                    document.title = (view.title)
                                        ? view.title + ' - ' + App.config.title
                                        : App.config.title;

                    //Set main view title
                    that.mainTitleContainer.html(view.title);

                    that.currentView.show.apply(that.currentView, routeArgs);
                });

            return this;
        },
    });
});